package com.llgz.hotelmanageapi.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.llgz.hotelmanageapi.pojo.DO.CommodityInfo;
import com.llgz.hotelmanageapi.pojo.DO.ConsumptionInfo;
import com.llgz.hotelmanageapi.mapper.ConsumptionInfoMapper;
import com.llgz.hotelmanageapi.service.ConsumptionInfoServer;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 客户消费表 服务实现类
 * </p>
 *
 * @author Bernie_fang
 * @since 2021-07-19
 */
@Service
public class ConsumptionInfoServiceImpl extends ServiceImpl<ConsumptionInfoMapper, ConsumptionInfo> implements ConsumptionInfoServer {

    @Autowired
    private CommodityInfoServiceImpl commodityInfoService;

    @Autowired
    private ConsumptionInfoMapper consumptionInfoMapper;

    @Override
    public List<List<Object>> getConsumptionDataByCommodityType(int commodityType) {
        List<List<Object>> allRowDataList = new LinkedList<>();

        QueryWrapper<CommodityInfo> commodityInfoQueryWrapper = new QueryWrapper<>();
        commodityInfoQueryWrapper.eq("commodity_code", commodityType);
        List<CommodityInfo> commodityInfoList = commodityInfoService.list(commodityInfoQueryWrapper);
        List<String> commodityNameList = commodityInfoList.stream().map(CommodityInfo::getCommodityName).collect(Collectors.toList());

        if (commodityNameList.size() <= 0) {
            return allRowDataList;
        }

        QueryWrapper<ConsumptionInfo> consumptionInfoQueryWrapper = new QueryWrapper<>();
        consumptionInfoQueryWrapper.in("commodity_name", commodityNameList);
        List<ConsumptionInfo> consumptionInfoList = consumptionInfoMapper.selectList(consumptionInfoQueryWrapper);

        for (ConsumptionInfo consumptionInfo : consumptionInfoList) {
            List<Object> rowData = new LinkedList<>();
            rowData.add(consumptionInfo.getCheckinId());
            rowData.add(consumptionInfo.getRoomId());
            rowData.add(consumptionInfo.getCommodityName());
            rowData.add(consumptionInfo.getQuantity());
            rowData.add(consumptionInfo.getConsumptionAmout() / consumptionInfo.getQuantity());
            rowData.add(consumptionInfo.getConsumptionAmout());
            rowData.add(consumptionInfo.getDiscountRate());
            rowData.add(consumptionInfo.getDiscountAmout());
            rowData.add(consumptionInfo.getSum());
            rowData.add(consumptionInfo.getOperator());
            rowData.add(consumptionInfo.getConsumptionTime());
            rowData.add(consumptionInfo.getRemarks());
            allRowDataList.add(rowData);
        }
        return allRowDataList;
    }

    @Override
    public List<List<String>> getConsumptionExcelHead() {
        List<List<String>> headList = new LinkedList<>();
        List<String> head0 = new LinkedList<>();
        head0.add("入住单号");
        List<String> head1 = new LinkedList<>();
        head1.add("客房编号");
        List<String> head2 = new LinkedList<>();
        head2.add("商品名");
        List<String> head3 = new LinkedList<>();
        head3.add("数量");
        List<String> head4 = new LinkedList<>();
        head4.add("单价");
        List<String> head5 = new LinkedList<>();
        head5.add("消费金额");
        List<String> head6 = new LinkedList<>();
        head6.add("折扣率");
        List<String> head7 = new LinkedList<>();
        head7.add("折后金额");
        List<String> head8 = new LinkedList<>();
        head8.add("合计");
        List<String> head9 = new LinkedList<>();
        head9.add("操作员");
        List<String> head10 = new LinkedList<>();
        head10.add("消费时间");
        List<String> head11 = new LinkedList<>();
        head11.add("备注");

        headList.add(head0);
        headList.add(head1);
        headList.add(head2);
        headList.add(head3);
        headList.add(head4);
        headList.add(head5);
        headList.add(head6);
        headList.add(head7);
        headList.add(head8);
        headList.add(head9);
        headList.add(head10);
        headList.add(head11);

        return headList;
    }

    @Override
    public List<List<Object>> getConsumptionDataByRoomId(int roomId) {

        List<List<Object>> allRowDataList = new LinkedList<>();

        QueryWrapper<ConsumptionInfo> consumptionInfoQueryWrapper = new QueryWrapper<>();
        consumptionInfoQueryWrapper.eq("room_id", roomId);
        List<ConsumptionInfo> consumptionInfoList = consumptionInfoMapper.selectList(consumptionInfoQueryWrapper);

        for (ConsumptionInfo consumptionInfo : consumptionInfoList) {
            List<Object> rowData = new LinkedList<>();
            rowData.add(consumptionInfo.getCheckinId());
            rowData.add(consumptionInfo.getRoomId());
            rowData.add(consumptionInfo.getCommodityName());
            rowData.add(consumptionInfo.getQuantity());
            rowData.add(consumptionInfo.getConsumptionAmout() / consumptionInfo.getQuantity());
            rowData.add(consumptionInfo.getConsumptionAmout());
            rowData.add(consumptionInfo.getDiscountRate());
            rowData.add(consumptionInfo.getDiscountAmout());
            rowData.add(consumptionInfo.getSum());
            rowData.add(consumptionInfo.getOperator());
            rowData.add(consumptionInfo.getConsumptionTime());
            rowData.add(consumptionInfo.getRemarks());
            allRowDataList.add(rowData);
        }
        return allRowDataList;
    }
}
